<?php
/***
***对接自我游系统
***Date 2015-10-09 
***Time pm 16:58
***User by  pzp
***/

include('/var/www/html/new/d/module/api/ZiWoYou/ziwoyou.class.php');
include('/var/www/html/new/d/module/api/function.class.php');
if (!class_exists("go_sql")) {
    include("/var/www/html/new/conf/le.je");
    $le = new go_sql();
    $le->connect();
}

$PFT_infunction = new AllFunction();
$PFT_infunction->logfile('5|'.json_encode($_REQUEST),'zwy_get_data');

if(!isset($_REQUEST['Action'])){
	$tid = $_REQUEST['TicketID'] + 0;
    // $select_uuid = "select uuid  from uu_jq_ticket where id=$tid limit 1";
    $select_uuid = "select t.uuid,l.apply_did  from uu_jq_ticket as t,uu_land as l where t.id=$tid and t.landid=l.id limit 1";
    $GLOBALS['le']->query($select_uuid);
    $GLOBALS['le']->fetch_assoc();
    $uuid = $GLOBALS['le']->f('uuid');
    $apply_did = $GLOBALS['le']->f('apply_did');
	
	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));
    $request_arr = array(
		'travel_date'	=> substr($_REQUEST['PlayDate'],0,10),
		'info_id'		=> $uuid,
		'order_source_id'=> $ls_order,
		'num'			=> $_REQUEST['Tnum'],
		'link_man'		=> $_REQUEST['TouristName'],
		'link_phone'	=> $_REQUEST['TouristTel'],
		'link_credit_no'=> $_REQUEST['IdCard'],
	);
	$zwy = new ZiWoYou($apply_did);
	
    $zwy_order =  $zwy->order($request_arr,$PFT_infunction);
	if($zwy_order == 101) exit("101");
	else{
		if($_REQUEST['PayStatus'] == 1){
			$zwy_pay = $zwy->OrderPay($zwy_order,$ls_order,$PFT_infunction);
			if($zwy_pay != 101){
				exit("200|0|0|0|".(string)$zwy_pay->code."|".(string)$zwy_pay->orderId."_".$ls_order);
			}
		}
		exit("200|0|0|0|0|".$zwy_order."_".$ls_order);
	}
}elseif($_REQUEST['Action'] == 'Relation'){ //下单成功

	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($Ordern);
	$UUtordernums = explode("_",(string)$pft_order_xml->Rec->UUtordernum);
	$apiOrder = $UUtordernums[0];
	$tempOrder = $UUtordernums[1];
	$vcode = (string)$pft_order_xml->Rec->UUvcode;
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	$UUpaystatus = (int)$pft_order_xml->Rec->UUpaystatus;
	if($UUpaystatus == 1){
		$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$Ordern','$apiOrder','$tempOrder',5,0,$tnum,'$vcode',0,now())";
		$GLOBALS['le']->query($str);
	}
}elseif($_REQUEST['Action'] == 'Relation_after_pay'){ //未支付订单支付通知
	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($Ordern);		
	$UUtordernums = explode("_",(string)$pft_order_xml->Rec->UUtordernum);	
	$apiOrder = $UUtordernums[0];
	$tempOrder = $UUtordernums[1];
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	$UUaids= (string)$pft_order_xml->Rec->UUaids;
	if($UUaids == 0){
		$UUaid = (int)$pft_order_xml->Rec->UUaid;
	}else{
		$UUaidss= explode(',',$UUaids);
		$UUaid = $UUaidss[0];
	}
	$zwy = new ZiWoYou($UUaid);
	
	$zwy_pay = $zwy->OrderPay($apiOrder,$PFT_infunction);
	if($zwy_pay != 101){
		$vcode = (string)$zwy_pay->code;
		$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$Ordern','$apiOrder','$tempOrder',5,0,$tnum,'$vcode',0,now())";
		$GLOBALS['le']->query($str);
	}
}elseif($_REQUEST['Action'] == 'MOD'){ //订单取消修改 	
	$Ordern = $_REQUEST['Ordern'];
	$selapiOrder = "select handleStatus,oStatus,oStnum from all_api_order where pftOrder='$Ordern' limit 1";
	$GLOBALS['le']->query($selapiOrder);
	if(!$GLOBALS['le']->fetch_assoc())	exit('105');
	$handleStatus = $GLOBALS['le']->f('handleStatus');
	$oStatus = $GLOBALS['le']->f('oStatus');
	$oStnum = $GLOBALS['le']->f('oStnum');
	
	if($oStatus == 3 && $handleStatus == 0) exit("200");
	if($oStatus == 4 && $handleStatus == 0 && $oStnum == $_REQUEST['Tnum']) exit("200");
	
	$pft_order_xml = $PFT_infunction->Select_PFTorder($Ordern);
	$UUtordernums = explode("_",(string)$pft_order_xml->Rec->UUtordernum);
	$apiOrder = $UUtordernums[0];
	$tempOrder = $UUtordernums[1];
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	$UUpaystatus = (int)$pft_order_xml->Rec->UUpaystatus;
	
	$UUaids= (string)$pft_order_xml->Rec->UUaids;
	if($UUaids == 0){
		$UUaid = (int)$pft_order_xml->Rec->UUaid;
	}else{
		$UUaidss= explode(',',$UUaids);
		$UUaid = $UUaidss[0];
	}
	
	$zwy = new ZiWoYou($UUaid);
	
	if($_REQUEST['Tnum'] == 0){ //取消订单
		$zwy_cancel = $zwy->OrderCancel($Ordern,$tempOrder,$apiOrder,$PFT_infunction);
		if($zwy_cancel == 100){
			$selorder = "update all_api_order set oStatus=3,handleStatus=0 where pftOrder='$Ordern' limit 1";
			$GLOBALS['le']->query($selorder);
			exit("200");
		}
		else exit("1077");
	}elseif($_REQUEST['Tnum'] > 0){ //修改订单
		if($UUpaystatus == 2) exit("1077"); //未支付订单没有修改数量的接口
		elseif($UUpaystatus == 1){
			$synum = $_REQUEST['Tnum'];
			$rnum  = $tnum - $synum;
			$OrderRefund = $zwy->OrderRefund($Ordern,$tempOrder,$apiOrder,$rnum,$PFT_infunction);
			if($OrderRefund == 100){
				$selorder = "update all_api_order set oStatus=4,handleStatus=0,oStnum=$synum  where pftOrder='$Ordern' limit 1";
				$GLOBALS['le']->query($selorder);
				exit("200");
			}
			else exit("1077");
		}
	}
}
?>